package zzxkj.blog.config;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;
import zzxkj.blog.schedules.DayViewCountSyncTask;
import zzxkj.blog.service.BlogService;
import zzxkj.blog.utils.EsUtil;

import javax.servlet.ServletContext;
/**
 * 容器启动后初始化数据
 * @author zzxkj
 */
@Component
public class ContextStartup  implements ApplicationRunner, ServletContextAware {
    ServletContext servletContext;
    final
    BlogService blogService;
    final
    DayViewCountSyncTask dayViewCountSyncTask;
    final
    EsUtil esUtil;

    public ContextStartup(BlogService blogService, DayViewCountSyncTask dayViewCountSyncTask, EsUtil esUtil) {
        this.blogService = blogService;
        this.dayViewCountSyncTask = dayViewCountSyncTask;
//        this.esUtils = esUtils;
        this.esUtil = esUtil;
    }

    /**
     * 启动时初始化各种数据
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception{
        //初始化榜单
        blogService.initWeekRank();
        //初始化后台数据
        blogService.initCacheChartData();
        //初始化天浏览量记录
        dayViewCountSyncTask.dayViewInit();
        //初始化es数据,默认10000条
        esUtil.esInit();
    }
    @Override
    public void setServletContext(ServletContext servletContext) {
           this.servletContext = servletContext;
    }
}
